package com.nuoniu.sibanyun.controller.production;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.MrpResultDto;
import com.nuoniu.sibanyun.entity.production.ErpMrpProgramme;
import com.nuoniu.sibanyun.entity.production.ErpMrpResult;
import com.nuoniu.sibanyun.service.production.IErpMrpProgrammeService;
import com.nuoniu.sibanyun.service.production.IErpMrpRecordService;
import com.nuoniu.sibanyun.service.production.IVProductionPlanMainService;
import com.nuoniu.sibanyun.service.so.IVSaleOrderMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * mrp运算
 * @auther 小啊刚
 * @date 2021-03-16
 */
@RequestMapping("/mrp")
@RestController
@Api(value = "mrp运算", tags = "mrp运算")
public class MrpController {

    @Autowired
    private IErpMrpRecordService mrpRecordService;
    @Autowired
    private IErpMrpProgrammeService mrpProgrammeService;
    @Autowired
    private IVSaleOrderMainService saleOrderMainService;
    @Autowired
    private IVProductionPlanMainService productionPlanMainService;

    @ApiOperation("获取方案列表")
    @GetMapping("getProgrammeList")
    public HttpResult getProgrammeList(Page page, ErpMrpProgramme mrpProgramme) {
        return HttpResult.ok(mrpProgrammeService.getProgrammeList(page, mrpProgramme));
    }

    @ApiOperation("保存方案")
    @PostMapping("save")
    public HttpResult save(@RequestBody ErpMrpProgramme mrpProgramme) {
        if(StringUtils.isBlank(mrpProgramme.getCode())) {
            throw new NuoNiuException("方案编码不能为空");
        }
        if(StringUtils.isBlank(mrpProgramme.getName())) {
            throw new NuoNiuException("方案名称不能为空");
        }
        if(StringUtils.isBlank(mrpProgramme.getWarehouseIds())) {
            throw new NuoNiuException("请选择仓库");
        }
        if(mrpProgramme.getId() != null && mrpProgramme.getId().equals(1)) {
            throw new NuoNiuException("系统方案不能修改");
        }

        mrpProgrammeService.saveMrpProgramme(mrpProgramme);
        return HttpResult.ok();
    }

    @ApiOperation("删除方案")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        if(id.equals(1)) {
            throw new NuoNiuException("系统方案不能删除");
        }

        mrpProgrammeService.removeById(id);
        return HttpResult.ok();
    }

    @ApiOperation("MRP运算")
    @PostMapping("operation")
    public HttpResult<ErpMrpResult> operation(@RequestBody ErpMrpProgramme mrpProgramme) {
        if(mrpProgramme == null) {
            throw new NuoNiuException("方案不能为空");
        }

        if(mrpProgramme.getMrpData() == null || mrpProgramme.getMrpData().size() == 0) {
            throw new NuoNiuException("运算数据不能为空");
        }

        return HttpResult.ok(mrpRecordService.mrpOperation(mrpProgramme.getMrpData(), mrpProgramme));
    }

    @ApiOperation("保存MRP运算结果")
    @PostMapping("saveMrpResult")
    public HttpResult saveMrpResult(@RequestBody MrpResultDto mrpResult) {
        mrpRecordService.saveResult(mrpResult);
        return HttpResult.ok();
    }

    @ApiOperation("获取销售订单列表")
    @GetMapping("getSaleOrderList")
    public HttpResult getSaleOrderList() {
        return HttpResult.ok(saleOrderMainService.getOrderList());
    }

    @ApiOperation("获取生产计划列表")
    @GetMapping("getProductionPlanList")
    public HttpResult getProductionPlanList() {
        return HttpResult.ok(productionPlanMainService.getOrderList());
    }

    @ApiOperation("获取运算编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok(mrpRecordService.getCode());
    }
}
